#include "electricpropertytablewidget.h"

#include <QTableWidgetItem>
#include <QComboBox>
#include <QHeaderView>

#include "svgnode.h"
#include "../appglobal.h"
#include "idevicetypeservice.h"

ElectricPropertyTableWidget::ElectricPropertyTableWidget(QWidget *parent) :
    QTableWidget(parent)
{
    mNode = NULL;
    mRowCnt = 0;
    this->setRowCount(mRowCnt);
    this->setColumnCount(3);

    QStringList headerLabels;
    headerLabels << tr("Type")<<tr("Property") <<tr("Value");

    this->setHorizontalHeaderLabels(headerLabels);
    this->horizontalHeader()->setStretchLastSection(true);
    this->verticalHeader()->setVisible(false);
	this->hideColumn(0);

	connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(slotUpdateAction()));
}

void ElectricPropertyTableWidget::initFromSvg(SvgNode *svg)
{
    if(!svg)
        return;

    mNode = svg;

    // clear all
	for (int i = 0; i < mRowCnt; i++)
	{
		this->removeRow(i);
	}
	mRowCnt = 0;

    bool bDefaultTypeExisted = false;
    GraphPrimitiveProp deviceProp;

    const QLinkedList<GraphPrimitiveProp>& props = svg->props();
    QLinkedList<GraphPrimitiveProp>::const_iterator begin = props.cbegin();
    QLinkedList<GraphPrimitiveProp>::const_iterator end = props.cend();
	// order
	int index = 0;
    // add normal items
    for(QLinkedList<GraphPrimitiveProp>::const_iterator itr = begin; itr != end; itr++,index++)
    {
        this->addProp((*itr));
    }

	this->sortItems(0, Qt::DescendingOrder);
}

void ElectricPropertyTableWidget::slotAdd()
{
	if (!mNode)
		return;
	
	this->addProp(GraphPrimitiveProp(tr("PropName"), GraphPrimitiveProp::STRING, tr("PropValue")));
}

void ElectricPropertyTableWidget::addProp(GraphPrimitiveProp prop)
{
	int row = mRowCnt++;
	this->setRowCount(mRowCnt);
	QTableWidgetItem *typeItem = new QTableWidgetItem(tr("%1").arg((int)prop.type()));
	this->setItem(row, 0, typeItem);
	QTableWidgetItem *nameItem = new QTableWidgetItem(tr("%1").arg(prop.name()));
	this->setItem(row, 1, nameItem);

	if (prop.type() == GraphPrimitiveProp::DEVICETYPE)
	{
		QComboBox *deviceType = new QComboBox(this);
		this->setCellWidget(row, 2, deviceType);

		const IDeviceTypeService &ser = AppGlobal::instance().serviceFactory().createDeviceTypeService();
        QList<QSharedPointer<DeviceType> > list = ser.List();
		for (int i = 0; i < list.count(); i++)
		{
            deviceType->insertItem(i, list.at(i)->name());
		}
		// store index
		deviceType->setCurrentIndex(prop.value().toInt());
	}
	else
	{
		QTableWidgetItem *valueItem = new QTableWidgetItem(tr("%1").arg(prop.value().toString()));
		this->setItem(row, 2, valueItem);
	}
}

void ElectricPropertyTableWidget::slotUpdateAction()
{
	emit sigSelectedItem(this->currentRow() < 0 ? false : true);
}

void ElectricPropertyTableWidget::slotRemove()
{
	int index = this->currentRow();
	if (index < 0)
		return;

	if (item(index, 0)->text().toInt() == (int)GraphPrimitiveProp::DEVICETYPE)
	{
		return;
	}

	mRowCnt--;
	if (mRowCnt < 0)
		mRowCnt = 0;

	this->removeRow(index);
}

void ElectricPropertyTableWidget::slotClear()
{
    for(int i = 0; i < mRowCnt; i++)
    {
		QTableWidgetItem* item = this->item(i, 0);
		if (item)
		{
			this->removeRow(i);
		}
        
    }

    mRowCnt = 0;
}

void ElectricPropertyTableWidget::slotSubmit()
{
    if(!mNode)
        return;

    // clear old
    mNode->clearProps();

    for(int i = 0; i < mRowCnt; i++)
    {
		GraphPrimitiveProp::PropType type = (GraphPrimitiveProp::PropType)this->item(i, 0)->text().toInt();
        QString name = this->item(i,1)->text();
        QVariant value;
		if (type == GraphPrimitiveProp::DEVICETYPE)
        {
            value = dynamic_cast<QComboBox*>(this->cellWidget(i,2))->currentIndex();
        }
        else
            value = this->item(i,2)->text();

        mNode->addProp(GraphPrimitiveProp(name,type,value));
    }
}
